﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using Bestlife.ERP.DataAccess;

/// <summary>
/// Summary description for D_Stock
/// </summary>
/// 
namespace Bestlife.ERP.DataAccess
{
    public class D_Stock
    {
        Decimal QuantityDB = 0;
        SqlDataReader rdR_Status;
        public D_Stock()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Decimal ReturnStockQuantity(string Pro_Name)
        {
            try
            {
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                String Ssql = "SELECT Qty_in_stock from Tbl_Stock  where Pro_Name='" + Pro_Name + "' ";
                //SqlDataReader ldr = Bestlife.ERP.DataAccess.clsCon.ReadTable(Ssql);


                rdR_Status = clsCon.ReadTable(Ssql);


                if (rdR_Status.Read())
                {
                    QuantityDB = Decimal.Parse(rdR_Status["Qty_in_stock"].ToString());

                }
            }catch(Exception)
            {
            
            }
            finally
            {
                rdR_Status.Close();
            }

            return QuantityDB;
        }

        public Boolean reduceStock(string Pro_Name, double qty)
        {
            Boolean upd2 = false;
            Decimal QuantityinDB = ReturnStockQuantity(Pro_Name);

            if (QuantityinDB > 0)
            {
                Decimal QuantityToDB = QuantityinDB - Decimal.Parse(qty.ToString());
                if (QuantityToDB > 0)
                {
                    String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                    Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                    String updtxt2 = "UPDATE Tbl_Stock SET  Qty_in_stock ='" + QuantityToDB + "',Last_Modify='" + System.DateTime.Now + "' WHERE Pro_Name='" + Pro_Name + "'";
                    // String updtxt2 = "UPDATE Bonus  SET  T_commision='999' WHERE User_id='001'";
                    upd2 = clsCon.AddEditDel(updtxt2);
                }
                else
                {
                    //print we have only QuantityinDB now !!!
                }
            }
            else
            {
                // sory we have nt any Product 
            }
            return upd2;
        }

    }
}